package com.jala.base;

import com.jala.ecs.client.session.data.SessionCache;
import com.jala.ecs.client.session.data.SessionMap;
import com.jala.umf.common.model.UserVO;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/13.
 */
@Component
public class Global {
    public final static String USER_SESSEION_KEY = "WX_LOGINFILTER_42B14496-45C1-E046-7097-533760DC11A5";

    private Logger logger= LoggerFactory.getLogger(Global.class);

    @Autowired
    private SessionCache sessionCache;

    public static UserVO getUserInfo() {

        Subject subject = SecurityUtils.getSubject();
        List list = subject.getPrincipals().asList(); //z,{login_id=z, user_name=测试用户, user_id=4518}
//	        String name = (String) list.get(0);
        Map<String, Object> attributes = (Map<String, Object>)list.get(1);
        UserVO ui = new UserVO();
        ui.setId(NumberUtils.toInt((String)attributes .get("user_id")));//用户ID
        ui.setFullName((String) attributes .get("user_name"));//用户名称
        ui.setUserName((String) attributes .get("login_id"));//用户的登陆账号

        return ui;
    }


    public  void setCacheValue(String key, Object value) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
//        WxSessionHttpServletRequestWrapper wxSessionHttpServletRequestWrapper=(WxSessionHttpServletRequestWrapper)request;
//        HttpSession session = wxSessionHttpServletRequestWrapper.getSession();
//        HttpSessionWrapper wrapper=(HttpSessionWrapper)session;
        SessionMap sessionMap=sessionCache.get(request.getSession().getId());
        sessionMap.setAttribute(key, value);
        sessionCache.put(request.getSession().getId(),sessionMap,7200);
//        ShopDetail shopDetail=(ShopDetail)sessionMap.getAttribute(USER_SESSEION_KEY);
//        wrapper.setAttribute(key, value);

    }

    public  Object getCacheValue(String key) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
//        WxSessionHttpServletRequestWrapper wxSessionHttpServletRequestWrapper=(WxSessionHttpServletRequestWrapper)request;
//        HttpSession session = wxSessionHttpServletRequestWrapper.getSession();
        SessionMap sessionMap=sessionCache.get(request.getSession().getId());
//        HttpSessionWrapper wrapper=(HttpSessionWrapper)session;
        Object object=(Object)sessionMap.getAttribute(key);
        return object;
    }

    public  void removeCacheValue(String key) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
//        WxSessionHttpServletRequestWrapper wxSessionHttpServletRequestWrapper=(WxSessionHttpServletRequestWrapper)request;
//        HttpSession session = wxSessionHttpServletRequestWrapper.getSession();
//        HttpSessionWrapper wrapper=(HttpSessionWrapper)session;
        SessionMap sessionMap=sessionCache.get(request.getSession().getId());
        sessionMap.removeAttribute(key);
        sessionCache.put(request.getSession().getId(),sessionMap,7200);
//        wrapper.removeAttribute(key);

    }
}
